package cn.com.qtgl.apis.iam.comp.service;

import cn.com.qtgl.apis.builtin.model.vo.Post;
import cn.com.qtgl.apis.iam.model.vo.IamApiResult;
import cn.com.qtgl.apis.iam.model.vo.PostEdges;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.graphql.comp.utils.GraphQLTools;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.cores.model.input.OrderByInput;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;

@Service
@WorkerComment("岗位信息")
@WorkerService(
        module = "apis",
        model = @WorkerModel(
                modelType = Post.class,dependenceTypes = {UserService.class}
        )
        ,ignores = {LOAD_ALL,LIST,SAVE,REMOVE_BY_ID,REMOVE_BY_ID_LIST,TOTAL_COUNT,GET_BY_ID}
)
public class PostService extends WorkerBasicService<Post> {

        @Resource
        private GraphQLTools graphQLTools;

        @Resource
        private IamCacheService iamCacheService;

        @WorkerQuery
        @WorkerComment("查询岗位信息")
        public <DATA extends PaginationList<Post>> DATA search(
                @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
                @WorkerParameter(value = "offset",comment = "偏移量",required = true) Long offset,
                @WorkerParameter(value = "filter",comment = "过滤",required = true) JsonMap<Object> filter,
                @WorkerParameter(value = "orderBy",comment = "排序") OrderByInput orderBy
        ) throws Exception {
                IamApiResult<PostEdges> posts=iamCacheService.posts();
                return (DATA) PaginationList.createOffset(posts.getEdges().stream().map(PostEdges::getNode).filter(node-> graphQLTools.filter(node,filter)).skip(offset).limit(first)
                        .collect(Collectors.toList()),0,posts.getEdges().size(),posts.getTotalCount());
        }
}
